//第三包 mymysql的一些例子
//这个具体的操作有点记不清了 以后再慢慢研究 虽然可以列出表字段 但是引入包比较多 而且搜索有些情况好像不太好

package main

import (
	//"database/sql"
	"fmt"
	//_ "github.com/ziutek/mymysql/godrv"
	"github.com/ziutek/mymysql/mysql"
	_ "github.com/ziutek/mymysql/native" // Native engine
	//"log"
	"os"
)

func main() {
	//db, err := sql.Open("mymysql", "tcp:127.0.0.1:3306*golang/root/")
	//if err != nil {
	//	log.Fatal(err)
	//}
	db := mysql.New("tcp", "", "127.0.0.1:3306", "root", "", "vgou3.0s")

	err := db.Connect()
	if err != nil {
		panic(err)
	}

	//id := 1
	//var query = "SELECT username from userinfo WHERE uid = ?"

	//rows, err := db.Query(query, id)
	//if err != nil {
	//	log.Fatal(err)
	//}

	//var username string
	//for rows.Next() {
	//	if err := rows.Scan(&username); err != nil {
	//		log.Fatal(err)
	//	}

	//	fmt.Printf("Email address: %s\n", username)

	//}

	rows, _, err := db.Query("select * from vg_order_info where id > %d", 1) //只有mysql.new的链接才能用三个参数 go自带的只有两个返回
	if err != nil {
		panic(err)
	}

	for _, row := range rows {
		for _, col := range row {
			if col == nil {
				// col has NULL value
			} else {
				// Do something with text in col (type []byte)
			}
		}
		// You can get specific value from a row
		//val1 := row[1].([]byte)

		// You can use it directly if conversion isn't needed
		//os.Stdout.Write(val1)

		// You can get converted value  按字段顺序赋值
		//uid := row.Int(0)        // Zero value
		//username := row.Str(1)   // First value
		//departname := row.Str(2) // Second value //这个str居然不支持中文
		//created := row.Date(3)   // Second value
		//fmt.Println(uid)
		//fmt.Println(username)
		//fmt.Println(departname)
		//fmt.Println(created)
		//fmt.Println("==================")

		// You may get values by column name //按字段名赋值
		//first := res.Map("uid")
		//second := res.Map("departname")
		//val1, val2 := row.Int(first), row.Str(second)
		//fmt.Println(val1, "==", val2)

		id := row[6].([]byte)
		os.Stdin.Write(id)
		fmt.Println("===")
		//fmt.Println(id)

	}

	//res1, err := db.Start("select * from userinfo")
	//checkError(err)

	// Print fields names
	//for _, field := range res1.Fields() {
	//	fmt.Print(field.Name, " ")
	//}
	//fmt.Println()

	//if err := rows.Err(); err != nil {
	//	log.Fatal(err)
	//}
}
